package com.ccrfid.rmc6.resource.device;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.device.LocatorEntity;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.resource.device.pojo.FloorPos;
import com.ccrfid.rmc6.service.device.LocatorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

@Path("/locators")
@Produces(MediaType.APPLICATION_JSON)
public class LocatorResource {
    private static final Logger logger = LoggerFactory.getLogger(LocatorResource.class);
    @Autowired
    LocatorService locatorService;

    @GET
    public List<LocatorEntity> getLocatorList(@QueryParam("floorNumber") Integer floorNumber,
                                              @QueryParam("pos") String pos) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (floorNumber != null) {
            criteria.add(new Criterion("floorNumber", Criterion.Operator.EQ, floorNumber));
        }
        if (pos != null) {
            if (pos.equalsIgnoreCase("Null")) {
                criteria.add(new Criterion("pos", Criterion.Operator.NULL, pos));
            } else if (pos.equalsIgnoreCase("NotNull")) {
                criteria.add(new Criterion("pos", Criterion.Operator.NOT_NULL, pos));
            }
        }
        List<LocatorEntity> list = locatorService.getLocatorList(criteria, Sort.by(new ArrayList<>()));
        return list;
    }

    @GET
    @Path("search")
    public Page<LocatorEntity> getLocatorInfoPage(
            @QueryParam("sn") String sn,
            @QueryParam("locatorType") Integer locatorType,
            @QueryParam("locatorAddress") Integer locatorAddress,
            @QueryParam("enabled") Integer enabled,
            @QueryParam("onlineStatus") Integer onlineStatus,
            @QueryParam("floorNumber") Integer floorNumber,
            @QueryParam("pos") String pos,
            @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (sn != null) {
            criteria.add(new Criterion("sn", Criterion.Operator.LIKE, '%' + sn + '%'));
        }
        if (locatorType != null) {
            criteria.add(new Criterion("locatorType", Criterion.Operator.EQ, locatorType));
        }
        if (locatorAddress != null) {
            criteria.add(new Criterion("locatorAddress", Criterion.Operator.EQ, locatorAddress));
        }
        if (enabled != null) {
            criteria.add(new Criterion("enabled", Criterion.Operator.EQ, enabled));
        }
        if (onlineStatus != null) {
            criteria.add(new Criterion("onlineStatus", Criterion.Operator.EQ, onlineStatus));
        }
        if (floorNumber != null) {
            criteria.add(new Criterion("floorNumber", Criterion.Operator.EQ, floorNumber));
        }
        if (pos != null) {
            if (pos.equalsIgnoreCase("Null")) {
                criteria.add(new Criterion("pos", Criterion.Operator.NULL, pos));
            } else if (pos.equalsIgnoreCase("NotNull")) {
                criteria.add(new Criterion("pos", Criterion.Operator.NOT_NULL, pos));
            }
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        Page<LocatorEntity> page = locatorService.getLocatorInfoPage(criteria, pageRequest);
        return page;
    }

    @GET
    @Path("{id}")
    public LocatorEntity getLocator(@PathParam("id") Long locatorId) {
        LocatorEntity locatorEntity = locatorService.getLocator(locatorId);
        return locatorEntity;
    }

    @PUT
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public LocatorEntity updateLocator(@PathParam("id") Long locatorId, LocatorEntity entity) throws AppException {
        LocatorEntity locatorEntity = locatorService.updateLocator(locatorId, entity);
        return locatorEntity;
    }

    @PUT
    @Path("{id}/position")
    @Consumes(MediaType.APPLICATION_JSON)
    public LocatorEntity updatePosition(@PathParam("id") Long locatorId, FloorPos floorPos) throws AppException {
        return locatorService.updatePosition(locatorId, floorPos);
    }

    @DELETE
    @Path("{id}/position")
    public Boolean deletePosition(@PathParam("id") Long locatorId) throws AppException {
        locatorService.deletePosition(locatorId);
        return true;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public LocatorEntity createLocator(LocatorEntity entity) throws AppException {
        LocatorEntity locatorEntity = locatorService.createLocator(entity);
        return locatorEntity;
    }

    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public int deleteLocatorsInfo(List<Long> idList) throws AppException {
        if (idList == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return locatorService.deleteLocatorsInfo(idList);
    }
}
